package mvc;

import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import engine.Triangle;
import engine.Vector;

/**
 * 
 * Controller component of the MVC system. This handles all user input, and
 * transmits it to the engine. The primary functionality should be setting up
 * all event listeners.
 * 
 * @author Dylan Swiggett
 * 
 */
public class Controller extends Thread {
	Model model;	//Has one way access to the Model
	View view;		//Has one way access to the View
	
	MouseListener mouseListener;
	KeyListener keyListener;
	
	KeyboardFocusManager keyManager;
	
	public Controller(Model model, View view){
		this.model = model;
		this.view = view;
		
		
		/*
		 * Set up all listeners for mouse and key input.
		 */
		mouseListener = new MouseListener(this);
		keyListener = new KeyListener(this);
		/*
		 * A Key Dispatcher doesn't rely on the focus.
		 * All key events will be captured.
		 */
		keyManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
		keyManager.addKeyEventDispatcher(keyListener);
		view.panel.addMouseListener(mouseListener);	//Panel or frame??? Decisions decisions...
	}
	
	public void run(){
		
	}
	
	/**
	 * Called by the Mouse Listener when the mouse is pressed.
	 * 
	 * @param evt
	 */
	public void mousePressed(MouseEvent evt){
		int x = evt.getX();
		int y = evt.getY();
		
		for (Triangle t : model.polygon.triangles){
			Vector exitPath = t.pointInTriangle(new Vector(x, y));
			if (exitPath != null){
				exitPath.print();

			}
		}
	}
	
	/**
	 * Called by the Mouse Listener when the mouse is released.
	 * 
	 * @param evt
	 */
	public void mouseReleased(MouseEvent evt){
		
	}
	
	/**
	 * Called by the Mouse Listener when the mouse is clicked.
	 * 
	 * @param evt
	 */
	public void mouseClicked(MouseEvent evt){
		
	}
	
	/**
	 * Called by the Mouse Listener when the mouse is dragged.
	 * 
	 * @param evt
	 */
	public void mouseDragged(MouseEvent evt){
		
	}
	
	/**
	 * Called by the Key Listener when a key is pressed.
	 * 
	 * @param evt
	 */
	public void keyPressed(KeyEvent evt){
		
	}
	
	/**
	 * Called by the Key Listener when a key is released.
	 * 
	 * @param evt
	 */
	public void keyReleased(KeyEvent evt){
		
	}
	
	/**
	 * Called by the Key Listener when a key is typed.
	 * 
	 * @param evt
	 */
	public void keyTyped(KeyEvent evt){
		if (evt.getKeyChar() == 'q'){
			System.exit(0);
		}
	}
}
